package com.problem.leetcode;

import com.google.common.base.Stopwatch;

import java.util.HashSet;

/**
 * @author didi
 * @DESCRIPTION
 * @create 2019-05-05 13:45
 **/

public class Problem202 {
    public boolean isHappy(int n) {
        HashSet<Integer> hashSet = new HashSet<>();
        while (n > 1) {
            n = powSum(n);
            if (hashSet.contains(n)) {
                return false;
            }
            hashSet.add(n);
        }
        return n == 1;
    }

    private int powSum(int n) {
        int sum = 0;
        while (n != 0) {
            int mode = n % 10;
            sum += mode * mode;
            n /= 10;
        }
        return sum;
    }

    public static void main(String[] args) {
        Stopwatch stopwatch = Stopwatch.createStarted();
        for (int index = 0; index < 10000000; index++) {
            if (new Problem202().isHappy(index)) {
                // System.out.println(index);
            }
            // System.out.println(index);
        }

        stopwatch.stop();
        System.out.println(stopwatch);
    }

}
